<?php

namespace common\jobs;

use Yii;
use yii\base\Exception;
use common\models\trade\HandleTrade;
use common\jobs\base\BaseRetryableJob;

/**
 * 交易自动过期处理
 * Class TradeExpiredJob.
 */
class TradeExpiredJob extends BaseRetryableJob {

    public $tradeid = null;

    /**
     * @param \yii\queue\Queue $queue
     * @return mixed|void
     * @throws Exception
     */
    public function execute($queue) {
        if ($this->tradeid == null) {
            throw new Exception('tradeid is null!');
        }
        $msg = '';
        $status = HandleTrade::cancelExpired($this->tradeid, $msg);
        Yii::error([
            'order_id' => $this->tradeid,
            'msg' => $msg,
        ], __METHOD__);
        if (!$status) {
            throw new Exception('[TRADEID:' . $this->tradeid . ']' . $msg);
        }
    }

    /**
     * @inheritdoc
     */
    public function getTtr() {
        return 60;
    }

    /**
     * @inheritdoc
     */
    public function canRetry($attempt, $error) {
        Yii::error($error, __METHOD__);
        return $attempt < 3;
    }

}
